package com.nondo.rdp.finance.endpoint.wap.app;

import com.nondo.rdp.finance.domain.Activity;
import com.nondo.rdp.finance.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.core.env.Environment;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller("apiWapActivity")
@RequestMapping("/wap/app/activity")
public class ActivityEndpoint {

    @Autowired
    ActivityService activityService;

    @Autowired
    private Environment environment;

    /**
     * 获取文章详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/info/{id}")
    public ModelAndView getActivityByType(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") long id) {
        ModelAndView mv = new ModelAndView("wap/app/activity/info");

        Map map = new HashMap();

        Activity article = this.activityService.findOne(id);
        if (article == null) {
            map.put("id", 0);
            map.put("title", "不存在的活动");
            map.put("body", "不存在的活动");
            map.put("valid", 0);
            map.put("startTime", 0);
            map.put("endTime", 0);
        } else {
            map.put("title", article.getTitle());
            map.put("body", article.getBody());
            map.put("id", article.getId());
            map.put("valid", article.getValid());
            map.put("startTime", article.getStartTime());
            map.put("endTime", article.getEndTime());
        }

        mv.addObject("activity", map);

        return mv;
    }

    @RequestMapping("/dynamic/{name}")
    public ModelAndView dynamicActivity(HttpServletRequest request, HttpServletResponse response, @PathVariable("name") String name) {
        ModelAndView mv = new ModelAndView();

        String path = "wap/app/activity/dynamic/" + name + "/index";
        String defaultPath = "wap/app/activity/dynamic/default/index";
        try {
            mv = new ModelAndView(path);
        } catch (Exception e) {
            mv = new ModelAndView(defaultPath);
        }

        mv.addObject("baseUrl", environment.getProperty("app.base_url"));

        return mv;
    }

    @RequestMapping("/dynamic/{name}/{type}")
    public ModelAndView dynamicActivityType(HttpServletRequest request, HttpServletResponse response, @PathVariable("name") String name, @PathVariable("type") String type) {
        ModelAndView mv = new ModelAndView();

        String path = "wap/app/activity/dynamic/" + name + "/" + type;
        String defaultPath = "wap/app/activity/dynamic/default/index";
        try {
            mv = new ModelAndView(path);
        } catch (Exception e) {
            mv = new ModelAndView(defaultPath);
        }

        mv.addObject("baseUrl", environment.getProperty("app.base_url"));

        return mv;
    }
}